import { flagDataObj } from './data'
import { getValType } from "../utils";

//将标签数据转换成树结构数据
export function convertFlagDataToTreeData(data) {
  let treeList = []
  Object.entries(data).forEach(([key, value]) => {
    let children = []
    value.forEach((item) => {
      if (item.flag) {
        let newChildren = item.flag.map((item) => {
          return { label: item, value: item }
        })
        children = [...children, ...newChildren]
      }
    })
    let treeItem = { label: key, value: key, children: children }
    treeList.push(treeItem)
  })
  return treeList
}

//将层级对象数据转换成树数据
export function convertDataToTreeData(data, parentKey = [], list = []) {
  Object.entries(data).forEach(([key, value]) => {
    if (getValType(value) === 'Object') {
      let parentKeyList = [...parentKey, key]
      let children = convertDataToTreeData(value, parentKeyList)
      let { flag, devPath, testPath } = flagDataObj[key] ? flagDataObj[key] : {}
      flag = flag ? flag : []
      let treeItem = {
        id: parentKeyList.join(','),
        label: key,
        flag,
        devPath,
        testPath,
        children: children
      }
      list.push(treeItem)
    }
    if (getValType(value) === 'Array') {
      let parentKeyList = [...parentKey, key]
      let children = value.map((item) => {
        let { flag, devPath, testPath } = flagDataObj[item] ? flagDataObj[item] : {}
        flag = flag ? flag : []
        return { id: `${parentKeyList.join(',')},${item}`, label: item, flag, devPath, testPath }
      })
      let { flag, devPath, testPath } = flagDataObj[key] ? flagDataObj[key] : {}
      flag = flag ? flag : []
      let treeItem = {
        id: `${parentKeyList.join(',')}`,
        label: key,
        flag,
        devPath,
        testPath,
        children
      }
      list.push(treeItem)
    }
  })
  return list
}

//将数据转换成一维数组
export function convertTreeDataToList(children, list = []) {
  children.forEach((item) => {
    if (item.hasOwnProperty('children')) {
      list.push({ ...item, children: 'undefined' })
      convertTreeDataToList(item.children, list)
    } else {
      list.push(item)
    }
  })
  return list
}
